<?php
namespace Spoof\Controller;
use Common\Controller\SpoofController;
class IndexController extends SpoofController {

//    public function __construct()
//    {
//        $sessionid = 'PHPSESSID='.$this->getSessionId();
//
//        $getsessionid = $_SERVER['HTTP_COOKIE'];
//        if ($sessionid!=$getsessionid){
//            $this->returnData('','状态已过期请重新授权',400);
//        }
//    }


    /**
     * 用户素材上传
     */
    public function getfile()
    {
        $appid = I('post.appid','','htmlspecialchars');
        $member_id = I('post.member_id','','int');
        if ($appid!="wxcd63916101feefd2"){
            $this->returnData('','非法请求',401);
        }
        $code = $_FILES['file'];//获取小程序传来的图片
        if (is_uploaded_file($_FILES['file']['tmp_name'])) {
            //把文件转存到你希望的目录（不要使用copy函数）
            $uploaded_file = $_FILES['file']['tmp_name'];

            $user_path = 'Public/spoof';

            if (!file_exists($user_path)) {
                mkdir($user_path);
            }

            $file_true_name = $_FILES['file']['name'];
            $move_to_file = $user_path . "/" . $file_true_name;
            $url = 'https://api.beikg.cn/'. $move_to_file;
            if (move_uploaded_file($uploaded_file, iconv("utf-8", "gb2312", $move_to_file))) {
                $ext=strrchr($url,'.');
                $data = array(
                    'spoof_member_id'=>$member_id,
                    'url'=>$url,
                    'name'=>$move_to_file,
                    'type'=>$_FILES['file']['type'],
                    'size'=>$_FILES['file']['size'],
                    'ext'=>$ext,
                    'savename'=>$file_true_name,
                    'savepath'=>$user_path,
                );
                M('spoof_uploadimg')->data($data)->add();
                $this->returnData(array('url' => $url));
            } else {
                $this->returnData('','上传失败',100);
            }

        }
    }

    /**
     * banner
     */
    public function getimg(){

        $data = array(
            '0'=> array(
                'appid'=>'wx6b1c7907c5733fc9',
                'url'=>'pages/index/index',
                'imgsrc'=>'http://oss.beikg.cn/appshow/egaobanner2.png',
            ),
            '1'=> array(
                'appid'=>'wx6b1c7907c5733fc9',
                'url'=>'pages/index/index',
                'imgsrc'=>'http://oss.beikg.cn/appshow/egaobenner.jpg',
            ),


    );

        $this->returnData($data,1);


    }

    /**
     * 保存恶搞图
     */
    public function uploadimg()
    {
        $appid = I('post.appid','','htmlspecialchars');
        $member_id = I('post.member_id','','int');
        if ($appid!="wxcd63916101feefd2"){
            $this->returnData('','非法请求',401);
        }
        $code = $_FILES['file'];//获取小程序传来的图片
        if (is_uploaded_file($_FILES['file']['tmp_name'])) {

            $uploaded_file = $_FILES['file']['tmp_name'];


            $user_path = 'Public/spoof';

            if (!file_exists($user_path)) {
                mkdir($user_path);
            }

            $file_true_name = $_FILES['file']['name'];
            $move_to_file = $user_path . "/" . $file_true_name;
            $url = 'https://api.beikg.cn/'. $move_to_file;
            if (move_uploaded_file($uploaded_file, iconv("utf-8", "gb2312", $move_to_file))) {
                $ext=strrchr($url,'.');
                $data = array(
                    'spoof_member_id'=>$member_id,
                    'url'=>$url,
                    'name'=>$move_to_file,
                    'type'=>$_FILES['file']['type'],
                    'size'=>$_FILES['file']['size'],
                    'ext'=>$ext,
                    'savename'=>$file_true_name,
                    'savepath'=>$user_path,
                    'spoof_imgtemplate_id'=>1
                );
                M('spoof_finalimg')->data($data)->add();
                $this->returnData(array('url' => $url));
            } else {
                $this->returnData('','上传失败',100);
            }

        }
    }

    /**
     * 存放模板消息
     */
    public function send()
    {
        $appid = I('post.appid','','htmlspecialchars');
        $touser = I('post.touser','','htmlspecialchars');
        $page = I('post.page','','htmlspecialchars');
        $form_id = I('post.form_id','','htmlspecialchars');
        $form_type = I('post.form_type','','htmlspecialchars');
        $secret = I('post.secret','','htmlspecialchars');
        $member_id = I('post.member_id','','int');
        $template_id = I('post.template_id','1','htmlspecialchars');

        if ($touser==""||$form_id==""){
            $this->returnData('','参数错误','401');
        }

        $date = date('Y-m-d');
        $insert = array(
            'appid'=>$appid,
            'touser'=>$touser,
            'template_id'=>$template_id,
            'page'=>$page,
            'form_id'=>$form_id,
            'form_type'=>$form_type?$form_type:1,
            'status'=>'-1',
            'add_time'=>time(),
            'secret'=>$secret,
            'createtime'=>$date,
            'spoof_member_id'=>$member_id,
        );
        M('spoof_template_send')->data($insert)->add();
        $this->returnData('成功','200');

        $where['touser'] = $touser;
        $where['createtime'] = $date;
        $where['status'] = '-1';

        $ishave = M('spoof_template_send')->field('wxopen_template_send_id')->where($where)->find();

        if ($ishave){
            M('spoof_template_send')->where(array('wxopen_template_send_id'=>$ishave['wxopen_template_send_id']))->save($insert);
        }else{
            M('spoof_template_send')->data($insert)->add();
        }

        $this->returnData('成功','200');

    }

//    /**
//     * 用户素材上传
//     */
//    public function getfile1()
//    {
//
//        $member_id = I('post.member_id','1','int');
//        if (empty($_FILES)){
//            $this->returnData('','非法请求',401);
//        }
//        if ($_FILES){
//
//            $setting= C('UPLOAD_SITEIMG_QINIU');
//            $setting['savePath'] = 'spoof/upload/';
//            $Upload = new \Think\Upload($setting);
//            $info = $Upload->upload($_FILES);
//            $file = $info['file'];
//            $data = array(
//                'url'=>$file['url'],
//                'name'=>$file['name'],
//                'type'=>$file['type'],
//                'size'=>$file['size'],
//                'ext'=>$file['ext'],
//                'savename'=>$file['savename'],
//                'savepath'=>$file['savepath'],
//                'spoof_member_id'=>$member_id,
//            );
//            M('spoof_uploadimg')->data($data)->add();
//            $this->returnData(array('url'=>$file['url']));
//        }
//
//    }

//    /**
//     * 用户生成的恶搞图
//     */
//    public function uploadfinalimg()
//    {
//
//        $member_id = I('post.member_id','1','int');
//        $imgtemplate_id = I('post.imgtemplate_id','','int');
//        if (empty($_FILES)){
//            $this->returnData('','非法请求',401);
//        }
//        if ($_FILES){
//
//            $setting= C('UPLOAD_SITEIMG_QINIU');
//            $setting['savePath'] = 'spoof/finalimg/';
//            $Upload = new \Think\Upload($setting);
//            $info = $Upload->upload($_FILES);
//            $file = $info['file'];
//            $data = array(
//                'url'=>$file['url'],
//                'name'=>$file['name'],
//                'type'=>$file['type'],
//                'size'=>$file['size'],
//                'ext'=>$file['ext'],
//                'savename'=>$file['savename'],
//                'savepath'=>$file['savepath'],
//                'spoof_member_id'=>$member_id,
//                'spoof_imgtemplate_id'=> $imgtemplate_id,
//            );
//            M('spoof_finalimg')->data($data)->add();
//            $this->returnData(array('url'=>$file['url']));
//        }
//
//    }

}